const { ccclass, property } = cc._decorator;

@ccclass
export default class WithdrawHistoryTs extends cc.Component {

    @property(cc.Label)
    labelTitle1: cc.Label = null;

    @property(cc.Label)
    labelNoRecord: cc.Label = null;

    @property(cc.Prefab)
    listItem: cc.Prefab = null;

    public itemDate: Array<any> = [];

    public imgBtnBg1: cc.Sprite = null;
    public imgBtnBg2: cc.Sprite = null;

    public labelBtnBg1: cc.Label = null;
    public labelBtnBg2: cc.Label = null;

    public clickBtnName: string = "";

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.imgBtnBg1 = this.node.getChildByName("groupAll").getChildByName("buttonAll").getChildByName("imgbtnBg1").getComponent(cc.Sprite);
        this.imgBtnBg2 = this.node.getChildByName("groupAll").getChildByName("buttonWon").getChildByName("imgbtnBg2").getComponent(cc.Sprite);

        this.labelBtnBg1 = this.node.getChildByName("groupAll").getChildByName("buttonAll").getChildByName("label").getComponent(cc.Label);
        this.labelBtnBg2 = this.node.getChildByName("groupAll").getChildByName("buttonWon").getChildByName("label").getComponent(cc.Label);

        this.onImgBtnBgIsShow(1);
    }

    public initWithdrawHistoryTs(resultData) {
        if (!this.labelNoRecord) return

        this.itemDate = resultData;
        this.itemDate.sort((a, b) => {
            return b.createdAt - a.createdAt;
        })
        cc.log(this.itemDate);
        if (this.itemDate.length > 0) {
            this.labelNoRecord.node.active = false;
        } else {
            this.labelNoRecord.node.active = true;
        }
        let content = this.node.getChildByName("groupAll").getChildByName("ScrollView").getChildByName("view").getChildByName("content");
        content.removeAllChildren();
        let aaa = this.itemDate;
        for (const k of this.itemDate) {
            let itemNode = cc.instantiate(this.listItem);
            content.addChild(itemNode);
            itemNode.getComponent("History_itemTs").setInfo(k);
        }
    }

    public onBtnClose() {
        // cc.gg.gameDataUtil.goToLobby(true);
        this.node.destroy();
    }

    public onBtnClick(event) {
        this.itemDate = [];
        let btnName = event.target.name;
        if (this.clickBtnName == btnName) return;
        if (btnName == "buttonAll") {
            this.getOtherHistory(1);
            this.onImgBtnBgIsShow(1);
        } else if (btnName == "buttonWon") {
            this.getOtherHistory(2);
            this.onImgBtnBgIsShow(2);
        }
        this.clickBtnName = btnName;
    }

    public onImgBtnBgIsShow(index: number) {
        this.imgBtnBg1.node.active = false;
        this.imgBtnBg2.node.active = false;

        this.labelBtnBg1.node.color = cc.Color.BLACK;
        this.labelBtnBg2.node.color = cc.Color.BLACK;

        this.labelTitle1.string = (index == 1) ? "WithdrawHistory" : "DepositHistory";

        this[`imgBtnBg${index}`].node.active = true;
        this[`labelBtnBg${index}`].node.color = cc.Color.WHITE;
    }

    /**获取订单历史
     * index      1=充值    2= 提现
     */
    public getOtherHistory(index: number) {
        cc.gg.httpUtil.httpGet('/order/history/list/get', { type: index }, function (result) {
            if (!result.succ) {
                this.itemDate = null;
            } else {
                this.initWithdrawHistoryTs(result.data);
            }
        }.bind(this));

    }




    // update (dt) {}
}
